﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Reflection;
using Pixysoft.Weblications.Sango.Environment;
using Pixysoft.Weblications.Sango.Dao;
using Pixysoft.Weblications.Sango.Constant;
using Pixysoft.Weblications.Sango.Formular;
using Pixysoft.Weblications.Sango.Protocol.player;
using Pixysoft.Weblications.Sango.Protocol;

namespace Pixysoft.Weblications.Sango.Command.player
{
    partial class SangoPlayerHandler
    {
        public player_donate player_donate()
        {
            string userCode = SangoManager.Authentication();

            player_donate response = ReflectionManager.CreatePojo<player_donate>();
            response.Succeed = false;

            Entities.Usr_Profile profile = UsrProfileDao.getUsrProfile(userCode);
            Entities.Usr_Property property = UsrPropertyDao.getProperty(userCode);
            Entities.Usr_Activity activity = UsrActivityDao.getHistory(userCode);

            Entities.Usr_Queue queue = UsrQueueDao.UsrQueueStartup(userCode, CtiQueueTypecodeCollection.DONATE, userCode, PlayerFormular.default_donate_cooltimespan);

            if (queue == null)
            {
                response.Message = "进贡冷却中";
                return response;
            }

            long soulvalue = PlayerFormular.getDonateRequiredSoulvalue(profile.Userlevel);

            // fame + party addition

            long famevalue = PlayerFormular.getDonateFamevalue(profile.Userlevel);
            Entities.Usr_Partymember member = PartyDao.tryGetActiveMember(userCode);
            if (member != null)
            {
                famevalue = PartyDao.getMissionAddition(member.Partyprimarycode, PtyMissionTypecodeCollection.DONATE, famevalue);
            }

            if (!UsrProfileDao.tryDecreaseSoulValue(profile, soulvalue))
            {
                response.Message = "军魂不足";
                return response;
            }

            IEntityNoebeTransaction transaction = SettingHelper.EntityTransaction;
            transaction.Begin(delegate()
            {
                if (UsrProfileDao.increaseFamevalue(profile, famevalue))
                {
                    Entities.Usr_Section section = UsrSectionDao.GetSection(userCode);

                    section.Scaletypecode = SysSectionScaleHelper.SearchByFamevalue(profile.Famevalue).Scaletypecode;

                    transaction.Update(section);
                }


                Entities.Sys_Famesetting nextSetting = SysFameSettingHelper.tryGetFrame(property.Fametitletypecode + 1);

                if (nextSetting != null &&
                    profile.Userlevel >= nextSetting.Requiredlevel &&
                    profile.Famevalue >= nextSetting.Requiredfamevalue)
                {
                    property.Fametitletypecode = nextSetting.Fametitletypecode;
                    transaction.Update(property);
                }

                transaction.Update(profile);

                transaction.Update(queue);

                transaction.Commit();
            });

            response.profile = UsrProfileFacade.ToProfile(profile);
            response.property = UsrProfileFacade.ToProperty(property);
            response.queue = UsrQueueFacade.ToQueue(queue);
            response.Succeed = true;
            return response;
        }


    }
    public interface player_donate : ISangoResponse
    {
        player_profile profile { get;set;}
        player_property property { get;set;}
        player_queue queue { get;set;}
    }
}
